package net.yrom.screenrecorder.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;

import net.yrom.screenrecorder.R;

import java.io.File;
import java.util.List;

public class VideoListActivity extends Activity implements AdapterView.OnItemClickListener {

    private ListView listView;
    private VideoAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView = new ListView(this);
        listView.setOnItemClickListener(this);
        setContentView(listView);
        loadScriptList();
    }

    private void loadScriptList() {
        File screenCaptures = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES),
                "ScreenCaptures");
        List<File> files = FileUtils.listFilesInDir(screenCaptures);
        adapter = new VideoAdapter(files);
        listView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this, VideoPlayerActivity.class);
        intent.putExtra("videopath", adapter.getItem(position).getAbsolutePath());
        startActivity(intent);
    }


    class VideoAdapter extends BaseAdapter {
        List<File> data;

        public VideoAdapter(List<File> data) {
            this.data = data;
        }

        @Override
        public int getCount() {
            if (data != null) return data.size();
            return 0;
        }

        @Override
        public File getItem(int position) {
            if (data != null) return data.get(position);
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(VideoListActivity.this, R.layout.item_video_list, null);
            TextView tvName = convertView.findViewById(R.id.item_name);
            tvName.setText(getItem(position).getName());
            return convertView;
        }
    }

}
